<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="支付类型" name="payType">
						<a-select v-model:value="searchFormState.payType" placeholder="请选择支付类型" :options="payTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="申请人" name="applicantName">
						<a-input v-model:value="searchFormState.applicantName" placeholder="请输入申请人" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="申请人部门" name="applicantDeptName">
						<a-input v-model:value="searchFormState.applicantDeptName" placeholder="请输入申请人部门" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="到访部门" name="visitDeptName">
						<a-input v-model:value="searchFormState.visitDeptName" placeholder="请输入到访部门" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="到访食堂" name="visitCafeteriaName">
						<a-input v-model:value="searchFormState.visitCafeteriaName" placeholder="请输入到访食堂" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="餐食类型" name="cafeteriaType">
						<a-select
							v-model:value="searchFormState.cafeteriaType"
							placeholder="请选择餐食类型"
							:options="cafeteriaTypeOptions"
						/>
					</a-form-item>
				</a-col>

				<a-col :span="6" v-show="advanced">
					<a-form-item label="支付时间" name="payTime">
						<a-range-picker v-model:value="searchFormState.payTime" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<xn-batch-button
						v-if="hasPerm('bizApprovalOrderBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchBizApprovalOrder"
					/>
				</a-space>
			</template>

			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'cafeteriaType'">
					{{ $TOOL.dictTypeData('CAFETERIA_TYPE', record.cafeteriaType) }}
				</template>
				<template v-if="column.dataIndex === 'payType'">
					{{ $TOOL.dictTypeData('PAY_TYPE', record.payType) }}
				</template>

				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizApprovalOrder(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizApprovalOrderDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="approvalorder">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizApprovalOrderApi from '@/api/biz/bizApprovalOrderApi'
	import tool from '@/utils/tool'
	const searchFormState = ref({ payType: 'TRUE' })
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '申请人',
			dataIndex: 'applicantName'
		},
		{
			title: '申请人部门',
			dataIndex: 'applicantDeptName'
		},
		{
			title: '到访部门',
			dataIndex: 'visitDeptName'
		},
		{
			title: '到访午餐食堂',
			dataIndex: 'visitCafeteriaName'
		},
		{
			title: '订单号',
			dataIndex: 'orderSn'
		},
		{
			title: '餐食类型',
			dataIndex: 'cafeteriaType'
		},
		{
			title: '餐食费用',
			dataIndex: 'cafeteriaPrice'
		},
		{
			title: '支付类型',
			dataIndex: 'payType'
		},
		{
			title: '支付时间',
			dataIndex: 'payTime'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizApprovalOrderEdit', 'bizApprovalOrderDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return bizApprovalOrderApi.bizApprovalOrderPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizApprovalOrder = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizApprovalOrderApi.bizApprovalOrderDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizApprovalOrder = (params) => {
		bizApprovalOrderApi.bizApprovalOrderDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const cafeteriaTypeOptions = tool.dictList('CAFETERIA_TYPE')
	const payTypeOptions = tool.dictList('PAY_TYPE')
</script>
